package org.szgb.console.zx.plan.web;

import cn.hutool.http.HttpStatus;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.szgb.console.sys.equipment.service.SysEquipmentService;
import org.szgb.console.sys.file.service.SysFileService;
import org.szgb.console.sys.line.service.SysLineLocationService;
import org.szgb.console.sys.todo.service.SysTodoService;
import org.szgb.console.sys.user.entity.SysUser;
import org.szgb.console.utils.PageView;
import org.szgb.console.utils.TokenUtil;
import org.szgb.console.zx.plan.entity.ZxPlan;
import org.szgb.console.zx.plan.service.ZxPlanService;
import org.szgb.core.base.entity.ApprovalDto;
import org.szgb.core.utils.AjaxJson;

import java.io.IOException;

@Slf4j
@RestController
@RequestMapping(value = "/zx/plan")
@Tag(name = "专项计划管理", description = "专项计划管理模块")
public class ZxPlanController {
    private final ZxPlanService zxPlanService;
    private final SysLineLocationService sysLineLocationService;
    private final SysEquipmentService sysEquipmentService;
    private final SysFileService sysFileService;

    @Autowired
    public ZxPlanController(ZxPlanService zxPlanService, SysLineLocationService sysLineLocationService, SysEquipmentService sysEquipmentService, SysFileService sysFileService) {
        this.zxPlanService = zxPlanService;
        this.sysLineLocationService = sysLineLocationService;
        this.sysEquipmentService = sysEquipmentService;
        this.sysFileService = sysFileService;
    }


    @Operation(summary = "获取专项计划信息", description = "根据查询参数获取专项计划信息，支持分页查询。使用id获取更详细的信息。")
    @PostMapping("/get")
    public AjaxJson get(@RequestBody(required = false) ZxPlan.ZxPlanFindDto findDto, HttpServletRequest request, HttpServletResponse response) {
        if (findDto == null) {
            findDto = new ZxPlan.ZxPlanFindDto();
        }
        // 创建一个PageView对象，用于分页显示
        PageView<ZxPlan.ZxPlanListView> pageView = new PageView<>();
        ZxPlan zxPlan = findDto.toZxPlan();
        // 如果实体对象的ID不为空，则直接调用get方法获取单个实体对象
        if (zxPlan != null && zxPlan.getId() != null && !zxPlan.getId().isEmpty()) {
            ZxPlan plan = zxPlanService.get(zxPlan.getId());
            // 如果实体对象为空，则返回错误响应
            if (plan == null) {
                return AjaxJson.error("数据不存在", HttpStatus.HTTP_NOT_FOUND, request, response);
            }

            // 返回成功的响应，并将实体对象放入响应中
            return AjaxJson.success(request, response).put("zxPlan", plan.toZxPlanView(String.join(",", sysLineLocationService.getNameListByIdList(plan.getSubstationIds().split(","))), String.join(",", sysEquipmentService.getNameListByIdArray(plan.getEquipmentIds().split(","))), plan.getPlanFileIds() == null ? null : sysFileService.getByIdArray(plan.getPlanFileIds().split(",")), plan.getTaskFileIds() == null ? null : sysFileService.getByIdArray(plan.getTaskFileIds().split(","))));
        } else {
            // 设置Page对象的列表属性，包含根据实体对象查询到的数据
            pageView.setList(ZxPlan.ZxPlanListView.getViewListFromList(zxPlanService.findList(zxPlan)));
            // 设置Page对象的总记录数属性，用于分页显示
            pageView.setCount(zxPlanService.getCount(zxPlan));
            // 创建一个成功的AjaxJson对象，并放入Page对象，以便于前端展示
            return AjaxJson.success(request, response).put("page", pageView);
        }
    }


    @PostMapping("/save")
    @Operation(summary = "保存专项计划", description = "新增或更改专项计划信息。")
    public AjaxJson save(@RequestBody ZxPlan.ZxPlanSaveDto zxPlanSaveDto, HttpServletRequest request, HttpServletResponse response) {
        if (zxPlanSaveDto == null) {
            return AjaxJson.error("参数错误", HttpStatus.HTTP_BAD_REQUEST, request, response);
        }
        ZxPlan zxPlan = zxPlanSaveDto.toZxPlan();
        // 设置实体对象的更新者为当前用户
        zxPlan.setUpdatedBy(new SysUser(TokenUtil.getUid(request)));
        // 调用服务层的save方法，尝试保存或更新实体对象，返回保存结果的Map
        zxPlanService.save(zxPlan);
        // 根据保存结果返回相应的AjaxJson对象
        return AjaxJson.success("保存成功", request, response).put("zxPlan", zxPlan.toZxPlanView(String.join(",", sysLineLocationService.getNameListByIdList(zxPlan.getSubstationIds().split(","))), String.join(",", sysEquipmentService.getNameListByIdArray(zxPlan.getEquipmentIds().split(","))), zxPlan.getPlanFileIds() == null ? null : sysFileService.getByIdArray(zxPlan.getPlanFileIds().split(",")), zxPlan.getTaskFileIds() == null ? null : sysFileService.getByIdArray(zxPlan.getTaskFileIds().split(","))));
    }

    @PostMapping("/create")
    @Operation(summary = "创建专项计划", description = "创建专项计划信息。")
    public AjaxJson create(@RequestBody ZxPlan.ZxPlanCreateDto zxPlanCreateDto, HttpServletRequest request, HttpServletResponse response) throws IOException, InterruptedException {
        ZxPlan zxPlan = zxPlanCreateDto.toZxPlan();
        zxPlan.setUpdatedBy(new SysUser(TokenUtil.getUid(request)));
        zxPlanService.create(zxPlan);
        // 根据保存结果返回相应的AjaxJson对象
        return AjaxJson.success("保存成功", request, response);
    }

    @PostMapping("/setLeader")
    @Operation(summary = "指定专项计划牵头人", description = "指定专项计划牵头人。")
    public AjaxJson setLeader(@RequestBody ZxPlan.ZxPlanSetLeaderDto zxPlanSetLeaderDto, HttpServletRequest request, HttpServletResponse response) throws IOException, InterruptedException {
        ZxPlan zxPlan = zxPlanSetLeaderDto.toZxPlan();
        // 设置实体对象的更新者为当前用户
        zxPlan.setUpdatedBy(new SysUser(TokenUtil.getUid(request)));
        zxPlanService.setLeader(zxPlan);
        // 根据保存结果返回相应的AjaxJson对象
        return AjaxJson.success("保存成功", request, response);
    }

    @PostMapping("/foremanApproval")
    @Operation(summary = "工长审批", description = "工长审批。")
    public AjaxJson foremanApproval(@RequestBody ApprovalDto approvalDto, HttpServletRequest request, HttpServletResponse response) throws IOException, InterruptedException {
        ZxPlan zxPlan = zxPlanService.get(approvalDto.getId());
        zxPlan.setUpdatedBy(new SysUser(TokenUtil.getUid(request)));
        zxPlanService.foremanApproval(approvalDto, zxPlan);
        return AjaxJson.success(request, response);
    }

    @PostMapping("/supervisorApproval")
    @Operation(summary = "技管审批", description = "技管审批。")
    public AjaxJson supervisorApproval(@RequestBody ApprovalDto approvalDto, HttpServletRequest request, HttpServletResponse response) throws IOException, InterruptedException {
        ZxPlan zxPlan = zxPlanService.get(approvalDto.getId());
        zxPlan.setUpdatedBy(new SysUser(TokenUtil.getUid(request)));
        zxPlanService.supervisorApproval(approvalDto, zxPlan);
        return AjaxJson.success(request, response);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除专项计划", description = "根据ID删除专项计划信息。", parameters = {@Parameter(name = "ids", description = "专项计划ID列表，多个ID用英文逗号分隔。")})
    public AjaxJson delete(@RequestParam String ids, HttpServletRequest request, HttpServletResponse response) {
        // 检查ID参数是否为空，为空则返回错误响应
        if (ids == null) {
            return AjaxJson.error("参数错误", HttpStatus.HTTP_BAD_REQUEST, request, response);
        }
        // 调用服务层方法，执行删除操作
        zxPlanService.delete(ids);
        // 返回成功响应
        return AjaxJson.success(request, response);
    }
}
